﻿using System.Collections.Generic;
using Cysharp.Threading.Tasks;
using IQIGame.Onigao.Framework;
using UnityEngine;

namespace IQIGame.Onigao.Game
{
    public class TimelineManager : FrameMonoModule
    {
        /// <summary>
        /// 内部Id生成器
        /// </summary>
        private static int s_SerialAssetID;

        public int timelineEffectGroupID = 13;

        public float InstanceAutoReleaseInterval = 60f;
        public float InstanceExpireTime = 60f;
        public int SameResInstanceCapacity = 1;
        public int TotalInstanceCapacity = 100;


        public TimelineGroup _timelineGroup { get; private set; }

        protected override void OnInit()
        {
            this._timelineGroup = new TimelineGroup(typeof(TimelineAssetInfo), this.InstanceAutoReleaseInterval, this.InstanceExpireTime,
                this.SameResInstanceCapacity, this.TotalInstanceCapacity);
            FrameworkEntry.MonoDriver.updateEvent += OnUpdate;
        }

        protected override void OnEnterGame()
        {
        }

        protected override void OnShutdown()
        {
            ManagerCenter.Entity.DisableGroup(this.timelineEffectGroupID);
        }

        private void OnUpdate(float deltaTime, float unScaledDeltaTime)
        {
            this._timelineGroup?.OnUpdate(deltaTime, unScaledDeltaTime);
        }

        public async UniTask<TimelineAssetInfo> Get(string entityAssetName)
        {
            TimelineAssetInfo rAsset = this._timelineGroup.Get(GenerateEntityID(), entityAssetName);
            await rAsset.Load();
            return rAsset;
        }

        /// <summary>
        /// 生成实体ID
        /// </summary>
        /// <param name="entityComponent"></param>
        /// <returns></returns>
        private static int GenerateEntityID()
        {
            return ++s_SerialAssetID;
        }
    }
}